<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <link rel="stylesheet" th:href="@{/bootstrap.min.css}" />
    <script th:src="@{/vue.js}"></script>
</head>
<body>

<div th:insert="publicHeader"/>
<script th:src="@{/publicHeader.js}"></script>



<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">

    <div class="container" v-if="products.length<=0" >
        <h3 style="text-align: center">很可惜，商品空空如也~</h3>
    </div>

    <div class="container" v-if="products.length>0">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px">所有商品</p>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6 col-md-4" v-for="product in products">
                <div class="thumbnail">
                    <img :src="product.productImgsrc" alt="..." class="col-sm-6 col-md-6">
                    <div class="caption text-left">
                        <p>商品名:<span v-text="product.productName">name</span></p>
                        <p>价格:<span v-text="product.productPrice + '元'">price</span></p>
                        <p>
                            <a class="btn btn-success" @click.prevent="addToCart(product.id)">加入购物车</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>


        <div class="row">
            <div class="col-md-12 col-md-offset-5 col-lg-offset-4col-xl-offset-5" >
                <ul class="pagination justify-content-center">
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage-1)">&laquo;</a></li>
                    <li><a href="javascript:void(0)" v-for="n in totalPages" v-text="n" @click.prevent="go(n)">n</a></li>
                    <li><a href="javascript:void(0)" @click.prevent="go(currentPage+1)" >&raquo;</a></li>
                </ul>
            </div>
        </div>

    </div>


</div>



<div th:insert="publicFooter"/>
<script th:src="@{/publicFooter.js}"></script>


</body>
</html>

<script>

    console.log(".........index...........")

    let indexVueObj = new Vue({
        el: "#app"
        ,
        data: {
            products:[],

            currentPage:1,
            pageSize:9,
            total:0,
            totalPages:0,

            keyword: '',
        }
        ,
        methods: {
            initAllProducts(){
                let _this=this;
                let params={
                    currentPage: _this.currentPage,
                    pageSize : _this.pageSize,
                    keyword: _this.keyword
                }
                console.log(params);
                $.post("/product/all",params,function (data) {

                    console.log(data)
                    _this.products=data.records;
                    _this.currentPage=data.current;
                    _this.pageSize=data.size;
                    //数据总量
                    _this.total=data.total;
                    //总页数
                    _this.totalPages=data.pages;
                })
            }
            ,
            go(pageNum){
                //小于1 或者 大于total 或者 仍获取当前页数据 直接返回
                if(pageNum<1 || pageNum>this.totalPages || pageNum==this.currentPage){
                    console.log(pageNum + " 无效页码值")
                    return;
                }

                this.currentPage = pageNum;
                this.initAllProducts();
            }
            ,
            addToCart(productId){
                console.log(productId)

                //判断当前是否是登录状态,如果未登录,跳转到登录页
                console.log(publicHeader)
                console.log(publicHeader._data.currentAccount)

                if(!publicHeader._data.currentAccount){
                    alert("请先登录");
                    location.href="/login.html";
                    return;
                }

                // 访问后端接口,把商品加入购物车
                $.get("/cart/add?productId="+productId,function (data) {
                    if(data == "ok"){
                        alert("加购成功")
                    }
                })
            }
        }
        ,
        created(){
            this.initAllProducts();
        }

    });


</script>